home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2000 March / maximum-cd-2000-03.iso / Quake3 Game Source / Q3AGameSource.exe / Main / g_mover.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-18  |  37.3 KB  |  1,465 lines

  1. // Copyright (C) 1999-2000 Id Software, Inc.
  2. //
  3.  
  4. #include "g_local.h"
  5.  
  6.  
  7.  
  8. /*
  9. ===============================================================================
  10.  
  11. PUSHMOVE
  12.  
  13. ===============================================================================
  14. */
  15.  
  16. void MatchTeam( gentity_t *teamLeader, int moverState, int time );
  17.  
  18. typedef struct {
  19.     gentity_t    *ent;
  20.     vec3_t    origin;
  21.     vec3_t    angles;
  22.     float    deltayaw;
  23. } pushed_t;
  24. pushed_t    pushed[MAX_GENTITIES], *pushed_p;
  25.  
  26.  
  27. /*
  28. ============
  29. G_TestEntityPosition
  30.  
  31. ============
  32. */
  33. gentity_t    *G_TestEntityPosition( gentity_t *ent ) {
  34.     trace_t    tr;
  35.     int        mask;
  36.  
  37.     if ( ent->clipmask ) {
  38.         mask = ent->clipmask;
  39.     } else {
  40.         mask = MASK_SOLID;
  41.     }
  42.     if ( ent->client ) {
  43.         trap_Trace( &tr, ent->client->ps.origin, ent->r.mins, ent->r.maxs, ent->client->ps.origin, ent->s.number, mask );
  44.     } else {
  45.         trap_Trace( &tr, ent->s.pos.trBase, ent->r.mins, ent->r.maxs, ent->s.pos.trBase, ent->s.number, mask );
  46.     }
  47.     
  48.     if (tr.startsolid)
  49.         return &g_entities[ tr.entityNum ];
  50.         
  51.     return NULL;
  52. }
  53.  
  54.  
  55. /*
  56. ==================
  57. G_TryPushingEntity
  58.  
  59. Returns qfalse if the move is blocked
  60. ==================
  61. */
  62. qboolean    G_TryPushingEntity( gentity_t *check, gentity_t *pusher, vec3_t move, vec3_t amove ) {
  63.     vec3_t        forward, right, up;
  64.     vec3_t        org, org2, move2;
  65.     gentity_t    *block;
  66.  
  67.     // EF_MOVER_STOP will just stop when contacting another entity
  68.     // instead of pushing it, but entities can still ride on top of it
  69.     if ( ( pusher->s.eFlags & EF_MOVER_STOP ) && 
  70.         check->s.groundEntityNum != pusher->s.number ) {
  71.         return qfalse;
  72.     }
  73.  
  74.     // save off the old position
  75.     if (pushed_p > &pushed[MAX_GENTITIES]) {
  76.         G_Error( "pushed_p > &pushed[MAX_GENTITIES]" );
  77.     }
  78.     pushed_p->ent = check;
  79.     VectorCopy (check->s.pos.trBase, pushed_p->origin);
  80.     VectorCopy (check->s.apos.trBase, pushed_p->angles);
  81.     if ( check->client ) {
  82.         pushed_p->deltayaw = check->client->ps.delta_angles[YAW];
  83.         VectorCopy (check->client->ps.origin, pushed_p->origin);
  84.     }
  85.     pushed_p++;
  86.  
  87.     // we need this for pushing things later
  88.     VectorSubtract (vec3_origin, amove, org);
  89.     AngleVectors (org, forward, right, up);
  90.  
  91.     // try moving the contacted entity 
  92.     VectorAdd (check->s.pos.trBase, move, check->s.pos.trBase);
  93.     if (check->client) {
  94.         // make sure the client's view rotates when on a rotating mover
  95.         check->client->ps.delta_angles[YAW] += ANGLE2SHORT(amove[YAW]);
  96.     }
  97.  
  98.     // figure movement due to the pusher's amove
  99.     VectorSubtract (check->s.pos.trBase, pusher->r.currentOrigin, org);
  100.     org2[0] = DotProduct (org, forward);
  101.     org2[1] = -DotProduct (org, right);
  102.     org2[2] = DotProduct (org, up);
  103.     VectorSubtract (org2, org, move2);
  104.     VectorAdd (check->s.pos.trBase, move2, check->s.pos.trBase);
  105.     if ( check->client ) {
  106.         VectorAdd (check->client->ps.origin, move, check->client->ps.origin);
  107.         VectorAdd (check->client->ps.origin, move2, check->client->ps.origin);
  108.     }
  109.  
  110.     // may have pushed them off an edge
  111.     if ( check->s.groundEntityNum != pusher->s.number ) {
  112.         check->s.groundEntityNum = -1;
  113.     }
  114.  
  115.     block = G_TestEntityPosition( check );
  116.     if (!block) {
  117.         // pushed ok
  118.         if ( check->client ) {
  119.             VectorCopy( check->client->ps.origin, check->r.currentOrigin );
  120.         } else {
  121.             VectorCopy( check->s.pos.trBase, check->r.currentOrigin );
  122.         }
  123.         trap_LinkEntity (check);
  124.         return qtrue;
  125.     }
  126.  
  127.     // if it is ok to leave in the old position, do it
  128.     // this is only relevent for riding entities, not pushed
  129.     // Sliding trapdoors can cause this.
  130.     VectorCopy( (pushed_p-1)->origin, check->s.pos.trBase);
  131.     if ( check->client ) {
  132.         VectorCopy( (pushed_p-1)->origin, check->client->ps.origin);
  133.     }
  134.     VectorCopy( (pushed_p-1)->angles, check->s.apos.trBase );
  135.     block = G_TestEntityPosition (check);
  136.     if ( !block ) {
  137.         check->s.groundEntityNum = -1;
  138.         pushed_p--;
  139.         return qtrue;
  140.     }
  141.  
  142.     // blocked
  143.     return qfalse;
  144. }
  145.  
  146.  
  147. /*
  148. ============
  149. G_MoverPush
  150.  
  151. Objects need to be moved back on a failed push,
  152. otherwise riders would continue to slide.
  153. If qfalse is returned, *obstacle will be the blocking entity
  154. ============
  155. */
  156. qboolean G_MoverPush( gentity_t *pusher, vec3_t move, vec3_t amove, gentity_t **obstacle ) {
  157.     int            i, e;
  158.     gentity_t    *check;
  159.     vec3_t        mins, maxs;
  160.     pushed_t    *p;
  161.     int            entityList[MAX_GENTITIES];
  162.     int            listedEntities;
  163.     vec3_t        totalMins, totalMaxs;
  164.  
  165.     *obstacle = NULL;
  166.  
  167.  
  168.     // mins/maxs are the bounds at the destination
  169.     // totalMins / totalMaxs are the bounds for the entire move
  170.     if ( pusher->r.currentAngles[0] || pusher->r.currentAngles[1] || pusher->r.currentAngles[2]
  171.         || amove[0] || amove[1] || amove[2] ) {
  172.         float        radius;
  173.  
  174.         radius = RadiusFromBounds( pusher->r.mins, pusher->r.maxs );
  175.         for ( i = 0 ; i < 3 ; i++ ) {
  176.             mins[i] = pusher->r.currentOrigin[i] + move[i] - radius;
  177.             maxs[i] = pusher->r.currentOrigin[i] + move[i] + radius;
  178.             totalMins[i] = mins[i] - move[i];
  179.             totalMaxs[i] = maxs[i] - move[i];
  180.         }
  181.     } else {
  182.         for (i=0 ; i<3 ; i++) {
  183.             mins[i] = pusher->r.absmin[i] + move[i];
  184.             maxs[i] = pusher->r.absmax[i] + move[i];
  185.         }
  186.  
  187.         VectorCopy( pusher->r.absmin, totalMins );
  188.         VectorCopy( pusher->r.absmax, totalMaxs );
  189.         for (i=0 ; i<3 ; i++) {
  190.             if ( move[i] > 0 ) {
  191.                 totalMaxs[i] += move[i];
  192.             } else {
  193.                 totalMins[i] += move[i];
  194.             }
  195.         }
  196.     }
  197.  
  198.     // unlink the pusher so we don't get it in the entityList
  199.     trap_UnlinkEntity( pusher );
  200.  
  201.     listedEntities = trap_EntitiesInBox( totalMins, totalMaxs, entityList, MAX_GENTITIES );
  202.  
  203.     // move the pusher to it's final position
  204.     VectorAdd( pusher->r.currentOrigin, move, pusher->r.currentOrigin );
  205.     VectorAdd( pusher->r.currentAngles, amove, pusher->r.currentAngles );
  206.     trap_LinkEntity( pusher );
  207.  
  208.     // see if any solid entities are inside the final position
  209.     for ( e = 0 ; e < listedEntities ; e++ ) {
  210.         check = &g_entities[ entityList[ e ] ];
  211.  
  212.         // only push items and players
  213.         if ( check->s.eType != ET_ITEM && check->s.eType != ET_PLAYER && !check->physicsObject ) {
  214.             continue;
  215.         }
  216.  
  217.         // if the entity is standing on the pusher, it will definitely be moved
  218.         if ( check->s.groundEntityNum != pusher->s.number ) {
  219.             // see if the ent needs to be tested
  220.             if ( check->r.absmin[0] >= maxs[0]
  221.             || check->r.absmin[1] >= maxs[1]
  222.             || check->r.absmin[2] >= maxs[2]
  223.             || check->r.absmax[0] <= mins[0]
  224.             || check->r.absmax[1] <= mins[1]
  225.             || check->r.absmax[2] <= mins[2] ) {
  226.                 continue;
  227.             }
  228.             // see if the ent's bbox is inside the pusher's final position
  229.             // this does allow a fast moving object to pass through a thin entity...
  230.             if (!G_TestEntityPosition (check)) {
  231.                 continue;
  232.             }
  233.         }
  234.  
  235.         // the entity needs to be pushed
  236.         if ( G_TryPushingEntity( check, pusher, move, amove ) ) {
  237.             continue;
  238.         }
  239.  
  240.         // the move was blocked an entity
  241.  
  242.         // bobbing entities are instant-kill and never get blocked
  243.         if ( pusher->s.pos.trType == TR_SINE || pusher->s.apos.trType == TR_SINE ) {
  244.             G_Damage( check, pusher, pusher, NULL, NULL, 99999, 0, MOD_CRUSH );
  245.             continue;
  246.         }
  247.  
  248.         
  249.         // save off the obstacle so we can call the block function (crush, etc)
  250.         *obstacle = check;
  251.  
  252.         // move back any entities we already moved
  253.         // go backwards, so if the same entity was pushed
  254.         // twice, it goes back to the original position
  255.         for ( p=pushed_p-1 ; p>=pushed ; p-- ) {
  256.             VectorCopy (p->origin, p->ent->s.pos.trBase);
  257.             VectorCopy (p->angles, p->ent->s.apos.trBase);
  258.             if ( p->ent->client ) {
  259.                 p->ent->client->ps.delta_angles[YAW] = p->deltayaw;
  260.                 VectorCopy (p->origin, p->ent->client->ps.origin);
  261.             }
  262.             trap_LinkEntity (p->ent);
  263.         }
  264.         return qfalse;
  265.     }
  266.  
  267.     return qtrue;
  268. }
  269.  
  270.  
  271. /*
  272. =================
  273. G_MoverTeam
  274. =================
  275. */
  276. void G_MoverTeam( gentity_t *ent ) {
  277.     vec3_t        move, amove;
  278.     gentity_t    *part, *obstacle;
  279.     vec3_t        origin, angles;
  280.  
  281.     obstacle = NULL;
  282.  
  283.     // make sure all team slaves can move before commiting
  284.     // any moves or calling any think functions
  285.     // if the move is blocked, all moved objects will be backed out
  286.     pushed_p = pushed;
  287.     for (part = ent ; part ; part=part->teamchain) {
  288.         // get current position
  289.         BG_EvaluateTrajectory( &part->s.pos, level.time, origin );
  290.         BG_EvaluateTrajectory( &part->s.apos, level.time, angles );
  291.         VectorSubtract( origin, part->r.currentOrigin, move );
  292.         VectorSubtract( angles, part->r.currentAngles, amove );
  293.         if ( !G_MoverPush( part, move, amove, &obstacle ) ) {
  294.             break;    // move was blocked
  295.         }
  296.     }
  297.  
  298.     if (part) {
  299.         // go back to the previous position
  300.         for ( part = ent ; part ; part = part->teamchain ) {
  301.             part->s.pos.trTime += level.time - level.previousTime;
  302.             part->s.apos.trTime += level.time - level.previousTime;
  303.             BG_EvaluateTrajectory( &part->s.pos, level.time, part->r.currentOrigin );
  304.             BG_EvaluateTrajectory( &part->s.apos, level.time, part->r.currentAngles );
  305.             trap_LinkEntity( part );
  306.         }
  307.  
  308.         // if the pusher has a "blocked" function, call it
  309.         if (ent->blocked) {
  310.             ent->blocked( ent, obstacle );
  311.         }
  312.         return;
  313.     }
  314.  
  315.     // the move succeeded
  316.     for ( part = ent ; part ; part = part->teamchain ) {
  317.         // call the reached function if time is at or past end point
  318.         if ( part->s.pos.trType == TR_LINEAR_STOP ) {
  319.             if ( level.time >= part->s.pos.trTime + part->s.pos.trDuration ) {
  320.                 if ( part->reached ) {
  321.                     part->reached( part );
  322.                 }
  323.             }
  324.         }
  325.     }
  326. }
  327.  
  328. /*
  329. ================
  330. G_RunMover
  331.  
  332. ================
  333. */
  334. void G_RunMover( gentity_t *ent ) {
  335.     // if not a team captain, don't do anything, because
  336.     // the captain will handle everything
  337.     if ( ent->flags & FL_TEAMSLAVE ) {
  338.         return;
  339.     }
  340.  
  341.     // if stationary at one of the positions, don't move anything
  342.     if ( ent->s.pos.trType != TR_STATIONARY || ent->s.apos.trType != TR_STATIONARY ) {
  343.         G_MoverTeam( ent );
  344.     }
  345.  
  346.     // check think function
  347.     G_RunThink( ent );
  348. }
  349.  
  350. /*
  351. ============================================================================
  352.  
  353. GENERAL MOVERS
  354.  
  355. Doors, plats, and buttons are all binary (two position) movers
  356. Pos1 is "at rest", pos2 is "activated"
  357. ============================================================================
  358. */
  359.  
  360. /*
  361. ===============
  362. SetMoverState
  363. ===============
  364. */
  365. void SetMoverState( gentity_t *ent, moverState_t moverState, int time ) {
  366.     vec3_t            delta;
  367.     float            f;
  368.  
  369.     ent->moverState = moverState;
  370.  
  371.     ent->s.pos.trTime = time;
  372.     switch( moverState ) {
  373.     case MOVER_POS1:
  374.         VectorCopy( ent->pos1, ent->s.pos.trBase );
  375.         ent->s.pos.trType = TR_STATIONARY;
  376.         break;
  377.     case MOVER_POS2:
  378.         VectorCopy( ent->pos2, ent->s.pos.trBase );
  379.         ent->s.pos.trType = TR_STATIONARY;
  380.         break;
  381.     case MOVER_1TO2:
  382.         VectorCopy( ent->pos1, ent->s.pos.trBase );
  383.         VectorSubtract( ent->pos2, ent->pos1, delta );
  384.         f = 1000.0 / ent->s.pos.trDuration;
  385.         VectorScale( delta, f, ent->s.pos.trDelta );
  386.         ent->s.pos.trType = TR_LINEAR_STOP;
  387.         break;
  388.     case MOVER_2TO1:
  389.         VectorCopy( ent->pos2, ent->s.pos.trBase );
  390.         VectorSubtract( ent->pos1, ent->pos2, delta );
  391.         f = 1000.0 / ent->s.pos.trDuration;
  392.         VectorScale( delta, f, ent->s.pos.trDelta );
  393.         ent->s.pos.trType = TR_LINEAR_STOP;
  394.         break;
  395.     }
  396.     BG_EvaluateTrajectory( &ent->s.pos, level.time, ent->r.currentOrigin );    
  397.     trap_LinkEntity( ent );
  398. }
  399.  
  400. /*
  401. ================
  402. MatchTeam
  403.  
  404. All entities in a mover team will move from pos1 to pos2
  405. in the same amount of time
  406. ================
  407. */
  408. void MatchTeam( gentity_t *teamLeader, int moverState, int time ) {
  409.     gentity_t        *slave;
  410.  
  411.     for ( slave = teamLeader ; slave ; slave = slave->teamchain ) {
  412.         SetMoverState( slave, moverState, time );
  413.     }
  414. }
  415.  
  416.  
  417.  
  418. /*
  419. ================
  420. ReturnToPos1
  421. ================
  422. */
  423. void ReturnToPos1( gentity_t *ent ) {
  424.     MatchTeam( ent, MOVER_2TO1, level.time );
  425.  
  426.     // looping sound
  427.     ent->s.loopSound = ent->soundLoop;
  428.  
  429.     // starting sound
  430.     if ( ent->sound2to1 ) {
  431.         G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );
  432.     }
  433. }
  434.  
  435.  
  436. /*
  437. ================
  438. Reached_BinaryMover
  439. ================
  440. */
  441. void Reached_BinaryMover( gentity_t *ent ) {
  442.  
  443.     // stop the looping sound
  444.     ent->s.loopSound = ent->soundLoop;
  445.  
  446.     if ( ent->moverState == MOVER_1TO2 ) {
  447.         // reached pos2
  448.         SetMoverState( ent, MOVER_POS2, level.time );
  449.  
  450.         // play sound
  451.         if ( ent->soundPos2 ) {
  452.             G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos2 );
  453.         }
  454.  
  455.         // return to pos1 after a delay
  456.         ent->think = ReturnToPos1;
  457.         ent->nextthink = level.time + ent->wait;
  458.  
  459.         // fire targets
  460.         if ( !ent->activator ) {
  461.             ent->activator = ent;
  462.         }
  463.         G_UseTargets( ent, ent->activator );
  464.     } else if ( ent->moverState == MOVER_2TO1 ) {
  465.         // reached pos1
  466.         SetMoverState( ent, MOVER_POS1, level.time );
  467.  
  468.         // play sound
  469.         if ( ent->soundPos1 ) {
  470.             G_AddEvent( ent, EV_GENERAL_SOUND, ent->soundPos1 );
  471.         }
  472.  
  473.         // close areaportals
  474.         if ( ent->teammaster == ent || !ent->teammaster ) {
  475.             trap_AdjustAreaPortalState( ent, qfalse );
  476.         }
  477.     } else {
  478.         G_Error( "Reached_BinaryMover: bad moverState" );
  479.     }
  480. }
  481.  
  482.  
  483. /*
  484. ================
  485. Use_BinaryMover
  486. ================
  487. */
  488. void Use_BinaryMover( gentity_t *ent, gentity_t *other, gentity_t *activator ) {
  489.     int        total;
  490.     int        partial;
  491.  
  492.     // only the master should be used
  493.     if ( ent->flags & FL_TEAMSLAVE ) {
  494.         Use_BinaryMover( ent->teammaster, other, activator );
  495.         return;
  496.     }
  497.  
  498.     ent->activator = activator;
  499.  
  500.     if ( ent->moverState == MOVER_POS1 ) {
  501.         // start moving 50 msec later, becase if this was player
  502.         // triggered, level.time hasn't been advanced yet
  503.         MatchTeam( ent, MOVER_1TO2, level.time + 50 );
  504.  
  505.         // starting sound
  506.         if ( ent->sound1to2 ) {
  507.             G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
  508.         }
  509.  
  510.         // looping sound
  511.         ent->s.loopSound = ent->soundLoop;
  512.  
  513.         // open areaportal
  514.         if ( ent->teammaster == ent || !ent->teammaster ) {
  515.             trap_AdjustAreaPortalState( ent, qtrue );
  516.         }
  517.         return;
  518.     }
  519.  
  520.     // if all the way up, just delay before coming down
  521.     if ( ent->moverState == MOVER_POS2 ) {
  522.         ent->nextthink = level.time + ent->wait;
  523.         return;
  524.     }
  525.  
  526.     // only partway down before reversing
  527.     if ( ent->moverState == MOVER_2TO1 ) {
  528.         total = ent->s.pos.trDuration;
  529.         partial = level.time - ent->s.time;
  530.         if ( partial > total ) {
  531.             partial = total;
  532.         }
  533.  
  534.         MatchTeam( ent, MOVER_1TO2, level.time - ( total - partial ) );
  535.  
  536.         if ( ent->sound1to2 ) {
  537.             G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound1to2 );
  538.         }
  539.         return;
  540.     }
  541.  
  542.     // only partway up before reversing
  543.     if ( ent->moverState == MOVER_1TO2 ) {
  544.         total = ent->s.pos.trDuration;
  545.         partial = level.time - ent->s.time;
  546.         if ( partial > total ) {
  547.             partial = total;
  548.         }
  549.  
  550.         MatchTeam( ent, MOVER_2TO1, level.time - ( total - partial ) );
  551.  
  552.         if ( ent->sound2to1 ) {
  553.             G_AddEvent( ent, EV_GENERAL_SOUND, ent->sound2to1 );
  554.         }
  555.         return;
  556.     }
  557. }
  558.  
  559.  
  560.  
  561. /*
  562. ================
  563. InitMover
  564.  
  565. "pos1", "pos2", and "speed" should be set before calling,
  566. so the movement delta can be calculated
  567. ================
  568. */
  569. void InitMover( gentity_t *ent ) {
  570.     vec3_t        move;
  571.     float        distance;
  572.     float        light;
  573.     vec3_t        color;
  574.     qboolean    lightSet, colorSet;
  575.     char        *sound;
  576.  
  577.     // if the "model2" key is set, use a seperate model
  578.     // for drawing, but clip against the brushes
  579.     if ( ent->model2 ) {
  580.         ent->s.modelindex2 = G_ModelIndex( ent->model2 );
  581.     }
  582.  
  583.     // if the "loopsound" key is set, use a constant looping sound when moving
  584.     if ( G_SpawnString( "noise", "100", &sound ) ) {
  585.         ent->s.loopSound = G_SoundIndex( sound );
  586.     }
  587.  
  588.     // if the "color" or "light" keys are set, setup constantLight
  589.     lightSet = G_SpawnFloat( "light", "100", &light );
  590.     colorSet = G_SpawnVector( "color", "1 1 1", color );
  591.     if ( lightSet || colorSet ) {
  592.         int        r, g, b, i;
  593.  
  594.         r = color[0] * 255;
  595.         if ( r > 255 ) {
  596.             r = 255;
  597.         }
  598.         g = color[1] * 255;
  599.         if ( g > 255 ) {
  600.             g = 255;
  601.         }
  602.         b = color[2] * 255;
  603.         if ( b > 255 ) {
  604.             b = 255;
  605.         }
  606.         i = light / 4;
  607.         if ( i > 255 ) {
  608.             i = 255;
  609.         }
  610.         ent->s.constantLight = r | ( g << 8 ) | ( b << 16 ) | ( i << 24 );
  611.     }
  612.  
  613.  
  614.     ent->use = Use_BinaryMover;
  615.     ent->reached = Reached_BinaryMover;
  616.  
  617.     ent->moverState = MOVER_POS1;
  618.     ent->r.svFlags = SVF_USE_CURRENT_ORIGIN;
  619.     ent->s.eType = ET_MOVER;
  620.     VectorCopy (ent->pos1, ent->r.currentOrigin);
  621.     trap_LinkEntity (ent);
  622.  
  623.     ent->s.pos.trType = TR_STATIONARY;
  624.     VectorCopy( ent->pos1, ent->s.pos.trBase );
  625.  
  626.     // calculate time to reach second position from speed
  627.     VectorSubtract( ent->pos2, ent->pos1, move );
  628.     distance = VectorLength( move );
  629.     if ( ! ent->speed ) {
  630.         ent->speed = 100;
  631.     }
  632.     VectorScale( move, ent->speed, ent->s.pos.trDelta );
  633.     ent->s.pos.trDuration = distance * 1000 / ent->speed;
  634.     if ( ent->s.pos.trDuration <= 0 ) {
  635.         ent->s.pos.trDuration = 1;
  636.     }
  637. }
  638.  
  639.  
  640. /*
  641. ===============================================================================
  642.  
  643. DOOR
  644.  
  645. A use can be triggered either by a touch function, by being shot, or by being
  646. targeted by another entity.
  647.  
  648. ===============================================================================
  649. */
  650.  
  651. /*
  652. ================
  653. Blocked_Door
  654. ================
  655. */
  656. void Blocked_Door( gentity_t *ent, gentity_t *other ) {
  657.     // remove anything other than a client
  658.     if ( !other->client ) {
  659.         // except CTF flags!!!!
  660.         if( other->s.eType == ET_ITEM && other->item->giType == IT_TEAM ) {
  661.             Team_DroppedFlagThink( other );
  662.             return;
  663.         }
  664.         G_TempEntity( other->s.origin, EV_ITEM_POP );
  665.         G_FreeEntity( other );
  666.         return;
  667.     }
  668.  
  669.     if ( ent->damage ) {
  670.         G_Damage( other, ent, ent, NULL, NULL, ent->damage, 0, MOD_CRUSH );
  671.     }
  672.     if ( ent->spawnflags & 4 ) {
  673.         return;        // crushers don't reverse
  674.     }
  675.  
  676.     // reverse direction
  677.     Use_BinaryMover( ent, ent, other );
  678. }
  679.  
  680. /*
  681. ================
  682. Touch_DoorTriggerSpectator
  683. ================
  684. */
  685. static void Touch_DoorTriggerSpectator( gentity_t *ent, gentity_t *other, trace_t *trace ) {
  686.     int i, axis;
  687.     vec3_t origin, dir, angles;
  688.  
  689.     axis = ent->count;
  690.     VectorClear(dir);
  691.     if (fabs(other->s.origin[axis] - ent->r.absmax[axis]) <
  692.         fabs(other->s.origin[axis] - ent->r.absmin[axis])) {
  693.         origin[axis] = ent->r.absmin[axis] - 10;
  694.         dir[axis] = -1;
  695.     }
  696.     else {
  697.         origin[axis] = ent->r.absmax[axis] + 10;
  698.         dir[axis] = 1;
  699.     }
  700.     for (i = 0; i < 3; i++) {
  701.         if (i == axis) continue;
  702.         origin[i] = (ent->r.absmin[i] + ent->r.absmax[i]) * 0.5;
  703.     }
  704.     vectoangles(dir, angles);
  705.     TeleportPlayer(other, origin, angles );
  706. }
  707.  
  708. /*
  709. ================
  710. Touch_DoorTrigger
  711. ================
  712. */
  713. void Touch_DoorTrigger( gentity_t *ent, gentity_t *other, trace_t *trace ) {
  714.     if ( other->client && other->client->sess.sessionTeam == TEAM_SPECTATOR ) {
  715.         // if the door is not open and not opening
  716.         if ( ent->parent->moverState != MOVER_1TO2 &&
  717.             ent->parent->moverState != MOVER_POS2) {
  718.             Touch_DoorTriggerSpectator( ent, other, trace );
  719.         }
  720.     }
  721.     else if ( ent->parent->moverState != MOVER_1TO2 ) {
  722.         Use_BinaryMover( ent->parent, ent, other );
  723.     }
  724. }
  725.  
  726.  
  727. /*
  728. ======================
  729. Think_SpawnNewDoorTrigger
  730.  
  731. All of the parts of a door have been spawned, so create
  732. a trigger that encloses all of them
  733. ======================
  734. */
  735. void Think_SpawnNewDoorTrigger( gentity_t *ent ) {
  736.     gentity_t        *other;
  737.     vec3_t        mins, maxs;
  738.     int            i, best;
  739.  
  740.     // set all of the slaves as shootable
  741.     for ( other = ent ; other ; other = other->teamchain ) {
  742.         other->takedamage = qtrue;
  743.     }
  744.  
  745.     // find the bounds of everything on the team
  746.     VectorCopy (ent->r.absmin, mins);
  747.     VectorCopy (ent->r.absmax, maxs);
  748.  
  749.     for (other = ent->teamchain ; other ; other=other->teamchain) {
  750.         AddPointToBounds (other->r.absmin, mins, maxs);
  751.         AddPointToBounds (other->r.absmax, mins, maxs);
  752.     }
  753.  
  754.     // find the thinnest axis, which will be the one we expand
  755.     best = 0;
  756.     for ( i = 1 ; i < 3 ; i++ ) {
  757.         if ( maxs[i] - mins[i] < maxs[best] - mins[best] ) {
  758.             best = i;
  759.         }
  760.     }
  761.     maxs[best] += 120;
  762.     mins[best] -= 120;
  763.  
  764.     // create a trigger with this size
  765.     other = G_Spawn ();
  766.     VectorCopy (mins, other->r.mins);
  767.     VectorCopy (maxs, other->r.maxs);
  768.     other->parent = ent;
  769.     other->r.contents = CONTENTS_TRIGGER;
  770.     other->touch = Touch_DoorTrigger;
  771.     // remember the thinnest axis
  772.     other->count = best;
  773.     trap_LinkEntity (other);
  774.  
  775.     MatchTeam( ent, ent->moverState, level.time );
  776. }
  777.  
  778. void Think_MatchTeam( gentity_t *ent ) {
  779.     MatchTeam( ent, ent->moverState, level.time );
  780. }
  781.  
  782.  
  783. /*QUAKED func_door (0 .5 .8) ? START_OPEN x CRUSHER
  784. TOGGLE        wait in both the start and end states for a trigger event.
  785. START_OPEN    the door to moves to its destination when spawned, and operate in reverse.  It is used to temporarily or permanently close off an area when triggered (not useful for touch or takedamage doors).
  786. NOMONSTER    monsters will not trigger this door
  787.  
  788. "model2"    .md3 model to also draw
  789. "angle"        determines the opening direction
  790. "targetname" if set, no touch field will be spawned and a remote button or trigger field activates the door.
  791. "speed"        movement speed (100 default)
  792. "wait"        wait before returning (3 default, -1 = never return)
  793. "lip"        lip remaining at end of move (8 default)
  794. "dmg"        damage to inflict when blocked (2 default)
  795. "color"        constantLight color
  796. "light"        constantLight radius
  797. "health"    if set, the door must be shot open
  798. */
  799. void SP_func_door (gentity_t *ent) {
  800.     vec3_t    abs_movedir;
  801.     float    distance;
  802.     vec3_t    size;
  803.     float    lip;
  804.  
  805.     ent->sound1to2 = ent->sound2to1 = G_SoundIndex("sound/movers/doors/dr1_strt.wav");
  806.     ent->soundPos1 = ent->soundPos2 = G_SoundIndex("sound/movers/doors/dr1_end.wav");
  807.  
  808.     ent->blocked = Blocked_Door;
  809.  
  810.     // default speed of 400
  811.     if (!ent->speed)
  812.         ent->speed = 400;
  813.  
  814.     // default wait of 2 seconds
  815.     if (!ent->wait)
  816.         ent->wait = 2;
  817.     ent->wait *= 1000;
  818.  
  819.     // default lip of 8 units
  820.     G_SpawnFloat( "lip", "8", &lip );
  821.  
  822.     // default damage of 2 points
  823.     G_SpawnInt( "dmg", "2", &ent->damage );
  824.  
  825.     // first position at start
  826.     VectorCopy( ent->s.origin, ent->pos1 );
  827.  
  828.     // calculate second position
  829.     trap_SetBrushModel( ent, ent->model );
  830.     G_SetMovedir (ent->s.angles, ent->movedir);
  831.     abs_movedir[0] = fabs(ent->movedir[0]);
  832.     abs_movedir[1] = fabs(ent->movedir[1]);
  833.     abs_movedir[2] = fabs(ent->movedir[2]);
  834.     VectorSubtract( ent->r.maxs, ent->r.mins, size );
  835.     distance = DotProduct( abs_movedir, size ) - lip;
  836.     VectorMA( ent->pos1, distance, ent->movedir, ent->pos2 );
  837.  
  838.     // if "start_open", reverse position 1 and 2
  839.     if ( ent->spawnflags & 1 ) {
  840.         vec3_t    temp;
  841.  
  842.         VectorCopy( ent->pos2, temp );
  843.         VectorCopy( ent->s.origin, ent->pos2 );
  844.         VectorCopy( temp, ent->pos1 );
  845.     }
  846.  
  847.     InitMover( ent );
  848.  
  849.     ent->nextthink = level.time + FRAMETIME;
  850.  
  851.     if ( ! (ent->flags & FL_TEAMSLAVE ) ) {
  852.         int health;
  853.  
  854.         G_SpawnInt( "health", "0", &health );
  855.         if ( health ) {
  856.             ent->takedamage = qtrue;
  857.         }
  858.         if ( ent->targetname || health ) {
  859.             // non touch/shoot doors
  860.             ent->think = Think_MatchTeam;
  861.         } else {
  862.             ent->think = Think_SpawnNewDoorTrigger;
  863.         }
  864.     }
  865.  
  866.  
  867. }
  868.  
  869. /*
  870. ===============================================================================
  871.  
  872. PLAT
  873.  
  874. ===============================================================================
  875. */
  876.  
  877. /*
  878. ==============
  879. Touch_Plat
  880.  
  881. Don't allow decent if a living player is on it
  882. ===============
  883. */
  884. void Touch_Plat( gentity_t *ent, gentity_t *other, trace_t *trace ) {
  885.     if ( !other->client || other->client->ps.stats[STAT_HEALTH] <= 0 ) {
  886.         return;
  887.     }
  888.  
  889.     // delay return-to-pos1 by one second
  890.     if ( ent->moverState == MOVER_POS2 ) {
  891.         ent->nextthink = level.time + 1000;
  892.     }
  893. }
  894.  
  895. /*
  896. ==============
  897. Touch_PlatCenterTrigger
  898.  
  899. If the plat is at the bottom position, start it going up
  900. ===============
  901. */
  902. void Touch_PlatCenterTrigger(gentity_t *ent, gentity_t *other, trace_t *trace ) {
  903.     if ( !other->client ) {
  904.         return;
  905.     }
  906.  
  907.     if ( ent->parent->moverState == MOVER_POS1 ) {
  908.         Use_BinaryMover( ent->parent, ent, other );
  909.     }
  910. }
  911.  
  912.  
  913. /*
  914. ================
  915. SpawnPlatTrigger
  916.  
  917. Spawn a trigger in the middle of the plat's low position
  918. Elevator cars require that the trigger extend through the entire low position,
  919. not just sit on top of it.
  920. ================
  921. */
  922. void SpawnPlatTrigger( gentity_t *ent ) {
  923.     gentity_t    *trigger;
  924.     vec3_t    tmin, tmax;
  925.  
  926.     // the middle trigger will be a thin trigger just
  927.     // above the starting position
  928.     trigger = G_Spawn();
  929.     trigger->touch = Touch_PlatCenterTrigger;
  930.     trigger->r.contents = CONTENTS_TRIGGER;
  931.     trigger->parent = ent;
  932.     
  933.     tmin[0] = ent->pos1[0] + ent->r.mins[0] + 33;
  934.     tmin[1] = ent->pos1[1] + ent->r.mins[1] + 33;
  935.     tmin[2] = ent->pos1[2] + ent->r.mins[2];
  936.  
  937.     tmax[0] = ent->pos1[0] + ent->r.maxs[0] - 33;
  938.     tmax[1] = ent->pos1[1] + ent->r.maxs[1] - 33;
  939.     tmax[2] = ent->pos1[2] + ent->r.maxs[2] + 8;
  940.  
  941.     if ( tmax[0] <= tmin[0] ) {
  942.         tmin[0] = ent->pos1[0] + (ent->r.mins[0] + ent->r.maxs[0]) *0.5;
  943.         tmax[0] = tmin[0] + 1;
  944.     }
  945.     if ( tmax[1] <= tmin[1] ) {
  946.         tmin[1] = ent->pos1[1] + (ent->r.mins[1] + ent->r.maxs[1]) *0.5;
  947.         tmax[1] = tmin[1] + 1;
  948.     }
  949.     
  950.     VectorCopy (tmin, trigger->r.mins);
  951.     VectorCopy (tmax, trigger->r.maxs);
  952.  
  953.     trap_LinkEntity (trigger);
  954. }
  955.  
  956.  
  957. /*QUAKED func_plat (0 .5 .8) ?
  958. Plats are always drawn in the extended position so they will light correctly.
  959.  
  960. "lip"        default 8, protrusion above rest position
  961. "height"    total height of movement, defaults to model height
  962. "speed"        overrides default 200.
  963. "dmg"        overrides default 2
  964. "model2"    .md3 model to also draw
  965. "color"        constantLight color
  966. "light"        constantLight radius
  967. */
  968. void SP_func_plat (gentity_t *ent) {
  969.     float        lip, height;
  970.  
  971.     ent->sound1to2 = ent->sound2to1 = G_SoundIndex("sound/movers/plats/pt1_strt.wav");
  972.     ent->soundPos1 = ent->soundPos2 = G_SoundIndex("sound/movers/plats/pt1_end.wav");
  973.  
  974.     VectorClear (ent->s.angles);
  975.  
  976.     G_SpawnFloat( "speed", "200", &ent->speed );
  977.     G_SpawnInt( "dmg", "2", &ent->damage );
  978.     G_SpawnFloat( "wait", "1", &ent->wait );
  979.     G_SpawnFloat( "lip", "8", &lip );
  980.  
  981.     ent->wait = 1000;
  982.  
  983.     // create second position
  984.     trap_SetBrushModel( ent, ent->model );
  985.  
  986.     if ( !G_SpawnFloat( "height", "0", &height ) ) {
  987.         height = (ent->r.maxs[2] - ent->r.mins[2]) - lip;
  988.     }
  989.  
  990.     // pos1 is the rest (bottom) position, pos2 is the top
  991.     VectorCopy( ent->s.origin, ent->pos2 );
  992.     VectorCopy( ent->pos2, ent->pos1 );
  993.     ent->pos1[2] -= height;
  994.  
  995.     InitMover( ent );
  996.  
  997.     // touch function keeps the plat from returning while
  998.     // a live player is standing on it
  999.     ent->touch = Touch_Plat;
  1000.  
  1001.     ent->blocked = Blocked_Door;
  1002.  
  1003.     ent->parent = ent;    // so it can be treated as a door
  1004.  
  1005.     // spawn the trigger if one hasn't been custom made
  1006.     if ( !ent->targetname ) {
  1007.         SpawnPlatTrigger(ent);
  1008.     }
  1009. }
  1010.  
  1011.  
  1012. /*
  1013. ===============================================================================
  1014.  
  1015. BUTTON
  1016.  
  1017. ===============================================================================
  1018. */
  1019.  
  1020. /*
  1021. ==============
  1022. Touch_Button
  1023.  
  1024. ===============
  1025. */
  1026. void Touch_Button(gentity_t *ent, gentity_t *other, trace_t *trace ) {
  1027.     if ( !other->client ) {
  1028.         return;
  1029.     }
  1030.  
  1031.     if ( ent->moverState == MOVER_POS1 ) {
  1032.         Use_BinaryMover( ent, other, other );
  1033.     }
  1034. }
  1035.  
  1036.  
  1037. /*QUAKED func_button (0 .5 .8) ?
  1038. When a button is touched, it moves some distance in the direction of it's angle, triggers all of it's targets, waits some time, then returns to it's original position where it can be triggered again.
  1039.  
  1040. "model2"    .md3 model to also draw
  1041. "angle"        determines the opening direction
  1042. "target"    all entities with a matching targetname will be used
  1043. "speed"        override the default 40 speed
  1044. "wait"        override the default 1 second wait (-1 = never return)
  1045. "lip"        override the default 4 pixel lip remaining at end of move
  1046. "health"    if set, the button must be killed instead of touched
  1047. "color"        constantLight color
  1048. "light"        constantLight radius
  1049. */
  1050. void SP_func_button( gentity_t *ent ) {
  1051.     vec3_t        abs_movedir;
  1052.     float        distance;
  1053.     vec3_t        size;
  1054.     float        lip;
  1055.  
  1056.     ent->sound1to2 = G_SoundIndex("sound/movers/switches/butn2.wav");
  1057.     
  1058.     if ( !ent->speed ) {
  1059.         ent->speed = 40;
  1060.     }
  1061.  
  1062.     if ( !ent->wait ) {
  1063.         ent->wait = 1;
  1064.     }
  1065.     ent->wait *= 1000;
  1066.  
  1067.     // first position
  1068.     VectorCopy( ent->s.origin, ent->pos1 );
  1069.  
  1070.     // calculate second position
  1071.     trap_SetBrushModel( ent, ent->model );
  1072.  
  1073.     G_SpawnFloat( "lip", "4", &lip );
  1074.  
  1075.     G_SetMovedir( ent->s.angles, ent->movedir );
  1076.     abs_movedir[0] = fabs(ent->movedir[0]);
  1077.     abs_movedir[1] = fabs(ent->movedir[1]);
  1078.     abs_movedir[2] = fabs(ent->movedir[2]);
  1079.     VectorSubtract( ent->r.maxs, ent->r.mins, size );
  1080.     distance = abs_movedir[0] * size[0] + abs_movedir[1] * size[1] + abs_movedir[2] * size[2] - lip;
  1081.     VectorMA (ent->pos1, distance, ent->movedir, ent->pos2);
  1082.  
  1083.     if (ent->health) {
  1084.         // shootable button
  1085.         ent->takedamage = qtrue;
  1086.     } else {
  1087.         // touchable button
  1088.         ent->touch = Touch_Button;
  1089.     }
  1090.  
  1091.     InitMover( ent );
  1092. }
  1093.  
  1094.  
  1095.  
  1096. /*
  1097. ===============================================================================
  1098.  
  1099. TRAIN
  1100.  
  1101. ===============================================================================
  1102. */
  1103.  
  1104.  
  1105. #define TRAIN_START_ON        1
  1106. #define TRAIN_TOGGLE        2
  1107. #define TRAIN_BLOCK_STOPS    4
  1108.  
  1109. /*
  1110. ===============
  1111. Think_BeginMoving
  1112.  
  1113. The wait time at a corner has completed, so start moving again
  1114. ===============
  1115. */
  1116. void Think_BeginMoving( gentity_t *ent ) {
  1117.     ent->s.pos.trTime = level.time;
  1118.     ent->s.pos.trType = TR_LINEAR_STOP;
  1119. }
  1120.  
  1121. /*
  1122. ===============
  1123. Reached_Train
  1124. ===============
  1125. */
  1126. void Reached_Train( gentity_t *ent ) {
  1127.     gentity_t        *next;
  1128.     float            speed;
  1129.     vec3_t            move;
  1130.     float            length;
  1131.  
  1132.     // copy the apropriate values
  1133.     next = ent->nextTrain;
  1134.     if ( !next || !next->nextTrain ) {
  1135.         return;        // just stop
  1136.     }
  1137.  
  1138.     // fire all other targets
  1139.     G_UseTargets( next, NULL );
  1140.  
  1141.     // set the new trajectory
  1142.     ent->nextTrain = next->nextTrain;
  1143.     VectorCopy( next->s.origin, ent->pos1 );
  1144.     VectorCopy( next->nextTrain->s.origin, ent->pos2 );
  1145.  
  1146.     // if the path_corner has a speed, use that
  1147.     if ( next->speed ) {
  1148.         speed = next->speed;
  1149.     } else {
  1150.         // otherwise use the train's speed
  1151.         speed = ent->speed;
  1152.     }
  1153.     if ( speed < 1 ) {
  1154.         speed = 1;
  1155.     }
  1156.  
  1157.     // calculate duration
  1158.     VectorSubtract( ent->pos2, ent->pos1, move );
  1159.     length = VectorLength( move );
  1160.  
  1161.     ent->s.pos.trDuration = length * 1000 / speed;
  1162.  
  1163.     // looping sound
  1164.     ent->s.loopSound = next->soundLoop;
  1165.  
  1166.     // start it going
  1167.     SetMoverState( ent, MOVER_1TO2, level.time );
  1168.  
  1169.     // if there is a "wait" value on the target, don't start moving yet
  1170.     if ( next->wait ) {
  1171.         ent->nextthink = level.time + next->wait * 1000;
  1172.         ent->think = Think_BeginMoving;
  1173.         ent->s.pos.trType = TR_STATIONARY;
  1174.     }
  1175. }
  1176.  
  1177.  
  1178. /*
  1179. ===============
  1180. Think_SetupTrainTargets
  1181.  
  1182. Link all the corners together
  1183. ===============
  1184. */
  1185. void Think_SetupTrainTargets( gentity_t *ent ) {
  1186.     gentity_t        *path, *next, *start;
  1187.  
  1188.     ent->nextTrain = G_Find( NULL, FOFS(targetname), ent->target );
  1189.     if ( !ent->nextTrain ) {
  1190.         G_Printf( "func_train at %s with an unfound target\n",
  1191.             vtos(ent->r.absmin) );
  1192.         return;
  1193.     }
  1194.  
  1195.     start = NULL;
  1196.     for ( path = ent->nextTrain ; path != start ; path = next ) {
  1197.         if ( !start ) {
  1198.             start = path;
  1199.         }
  1200.  
  1201.         if ( !path->target ) {
  1202.             G_Printf( "Train corner at %s without a target\n",
  1203.                 vtos(path->s.origin) );
  1204.             return;
  1205.         }
  1206.  
  1207.         // find a path_corner among the targets
  1208.         // there may also be other targets that get fired when the corner
  1209.         // is reached
  1210.         next = NULL;
  1211.         do {
  1212.             next = G_Find( next, FOFS(targetname), path->target );
  1213.             if ( !next ) {
  1214.                 G_Printf( "Train corner at %s without a target path_corner\n",
  1215.                     vtos(path->s.origin) );
  1216.                 return;
  1217.             }
  1218.         } while ( strcmp( next->classname, "path_corner" ) );
  1219.  
  1220.         path->nextTrain = next;
  1221.     }
  1222.  
  1223.     // start the train moving from the first corner
  1224.     Reached_Train( ent );
  1225. }
  1226.  
  1227.  
  1228.  
  1229. /*QUAKED path_corner (.5 .3 0) (-8 -8 -8) (8 8 8)
  1230. Train path corners.
  1231. Target: next path corner and other targets to fire
  1232. "speed" speed to move to the next corner
  1233. "wait" seconds to wait before behining move to next corner
  1234. */
  1235. void SP_path_corner( gentity_t *self ) {
  1236.     if ( !self->targetname ) {
  1237.         G_Printf ("path_corner with no targetname at %s\n", vtos(self->s.origin));
  1238.         G_FreeEntity( self );
  1239.         return;
  1240.     }
  1241.     // path corners don't need to be linked in
  1242. }
  1243.  
  1244.  
  1245.  
  1246. /*QUAKED func_train (0 .5 .8) ? START_ON TOGGLE BLOCK_STOPS
  1247. A train is a mover that moves between path_corner target points.
  1248. Trains MUST HAVE AN ORIGIN BRUSH.
  1249. The train spawns at the first target it is pointing at.
  1250. "model2"    .md3 model to also draw
  1251. "speed"        default 100
  1252. "dmg"        default    2
  1253. "noise"        looping sound to play when the train is in motion
  1254. "target"    next path corner
  1255. "color"        constantLight color
  1256. "light"        constantLight radius
  1257. */
  1258. void SP_func_train (gentity_t *self) {
  1259.     VectorClear (self->s.angles);
  1260.  
  1261.     if (self->spawnflags & TRAIN_BLOCK_STOPS) {
  1262.         self->damage = 0;
  1263.     } else {
  1264.         if (!self->damage) {
  1265.             self->damage = 2;
  1266.         }
  1267.     }
  1268.  
  1269.     if ( !self->speed ) {
  1270.         self->speed = 100;
  1271.     }
  1272.  
  1273.     if ( !self->target ) {
  1274.         G_Printf ("func_train without a target at %s\n", vtos(self->r.absmin));
  1275.         G_FreeEntity( self );
  1276.         return;
  1277.     }
  1278.  
  1279.     trap_SetBrushModel( self, self->model );
  1280.     InitMover( self );
  1281.  
  1282.     self->reached = Reached_Train;
  1283.  
  1284.     // start trains on the second frame, to make sure their targets have had
  1285.     // a chance to spawn
  1286.     self->nextthink = level.time + FRAMETIME;
  1287.     self->think = Think_SetupTrainTargets;
  1288. }
  1289.  
  1290. /*
  1291. ===============================================================================
  1292.  
  1293. STATIC
  1294.  
  1295. ===============================================================================
  1296. */
  1297.  
  1298.  
  1299. /*QUAKED func_static (0 .5 .8) ?
  1300. A bmodel that just sits there, doing nothing.  Can be used for conditional walls and models.
  1301. "model2"    .md3 model to also draw
  1302. "color"        constantLight color
  1303. "light"        constantLight radius
  1304. */
  1305. void SP_func_static( gentity_t *ent ) {
  1306.     trap_SetBrushModel( ent, ent->model );
  1307.     InitMover( ent );
  1308.     VectorCopy( ent->s.origin, ent->s.pos.trBase );
  1309.     VectorCopy( ent->s.origin, ent->r.currentOrigin );
  1310. }
  1311.  
  1312.  
  1313. /*
  1314. ===============================================================================
  1315.  
  1316. ROTATING
  1317.  
  1318. ===============================================================================
  1319. */
  1320.  
  1321.  
  1322. /*QUAKED func_rotating (0 .5 .8) ? START_ON - X_AXIS Y_AXIS
  1323. You need to have an origin brush as part of this entity.  The center of that brush will be
  1324. the point around which it is rotated. It will rotate around the Z axis by default.  You can
  1325. check either the X_AXIS or Y_AXIS box to change that.
  1326.  
  1327. "model2"    .md3 model to also draw
  1328. "speed"        determines how fast it moves; default value is 100.
  1329. "dmg"        damage to inflict when blocked (2 default)
  1330. "color"        constantLight color
  1331. "light"        constantLight radius
  1332. */
  1333. void SP_func_rotating (gentity_t *ent) {
  1334.     if ( !ent->speed ) {
  1335.         ent->speed = 100;
  1336.     }
  1337.  
  1338.     // set the axis of rotation
  1339.     ent->s.apos.trType = TR_LINEAR;
  1340.     if ( ent->spawnflags & 4 ) {
  1341.         ent->s.apos.trDelta[2] = ent->speed;
  1342.     } else if ( ent->spawnflags & 8 ) {
  1343.         ent->s.apos.trDelta[0] = ent->speed;
  1344.     } else {
  1345.         ent->s.apos.trDelta[1] = ent->speed;
  1346.     }
  1347.  
  1348.     if (!ent->damage) {
  1349.         ent->damage = 2;
  1350.     }
  1351.  
  1352.     trap_SetBrushModel( ent, ent->model );
  1353.     InitMover( ent );
  1354.  
  1355.     VectorCopy( ent->s.origin, ent->s.pos.trBase );
  1356.     VectorCopy( ent->s.pos.trBase, ent->r.currentOrigin );
  1357.     VectorCopy( ent->s.apos.trBase, ent->r.currentAngles );
  1358.  
  1359.     trap_LinkEntity( ent );
  1360. }
  1361.  
  1362.  
  1363. /*
  1364. ===============================================================================
  1365.  
  1366. BOBBING
  1367.  
  1368. ===============================================================================
  1369. */
  1370.  
  1371.  
  1372. /*QUAKED func_bobbing (0 .5 .8) ? X_AXIS Y_AXIS
  1373. Normally bobs on the Z axis
  1374. "model2"    .md3 model to also draw
  1375. "height"    amplitude of bob (32 default)
  1376. "speed"        seconds to complete a bob cycle (4 default)
  1377. "phase"        the 0.0 to 1.0 offset in the cycle to start at
  1378. "dmg"        damage to inflict when blocked (2 default)
  1379. "color"        constantLight color
  1380. "light"        constantLight radius
  1381. */
  1382. void SP_func_bobbing (gentity_t *ent) {
  1383.     float        height;
  1384.     float        phase;
  1385.  
  1386.     G_SpawnFloat( "speed", "4", &ent->speed );
  1387.     G_SpawnFloat( "height", "32", &height );
  1388.     G_SpawnInt( "dmg", "2", &ent->damage );
  1389.     G_SpawnFloat( "phase", "0", &phase );
  1390.  
  1391.     trap_SetBrushModel( ent, ent->model );
  1392.     InitMover( ent );
  1393.  
  1394.     VectorCopy( ent->s.origin, ent->s.pos.trBase );
  1395.     VectorCopy( ent->s.origin, ent->r.currentOrigin );
  1396.  
  1397.     ent->s.pos.trDuration = ent->speed * 1000;
  1398.     ent->s.pos.trTime = ent->s.pos.trDuration * phase;
  1399.     ent->s.pos.trType = TR_SINE;
  1400.  
  1401.     // set the axis of bobbing
  1402.     if ( ent->spawnflags & 1 ) {
  1403.         ent->s.pos.trDelta[0] = height;
  1404.     } else if ( ent->spawnflags & 2 ) {
  1405.         ent->s.pos.trDelta[1] = height;
  1406.     } else {
  1407.         ent->s.pos.trDelta[2] = height;
  1408.     }
  1409. }
  1410.  
  1411. /*
  1412. ===============================================================================
  1413.  
  1414. PENDULUM
  1415.  
  1416. ===============================================================================
  1417. */
  1418.  
  1419.  
  1420. /*QUAKED func_pendulum (0 .5 .8) ?
  1421. You need to have an origin brush as part of this entity.
  1422. Pendulums always swing north / south on unrotated models.  Add an angles field to the model to allow rotation in other directions.
  1423. Pendulum frequency is a physical constant based on the length of the beam and gravity.
  1424. "model2"    .md3 model to also draw
  1425. "speed"        the number of degrees each way the pendulum swings, (30 default)
  1426. "phase"        the 0.0 to 1.0 offset in the cycle to start at
  1427. "dmg"        damage to inflict when blocked (2 default)
  1428. "color"        constantLight color
  1429. "light"        constantLight radius
  1430. */
  1431. void SP_func_pendulum(gentity_t *ent) {
  1432.     float        freq;
  1433.     float        length;
  1434.     float        phase;
  1435.     float        speed;
  1436.  
  1437.     G_SpawnFloat( "speed", "30", &speed );
  1438.     G_SpawnInt( "dmg", "2", &ent->damage );
  1439.     G_SpawnFloat( "phase", "0", &phase );
  1440.  
  1441.     trap_SetBrushModel( ent, ent->model );
  1442.  
  1443.     // find pendulum length
  1444.     length = fabs( ent->r.mins[2] );
  1445.     if ( length < 8 ) {
  1446.         length = 8;
  1447.     }
  1448.  
  1449.     freq = 1 / ( M_PI * 2 ) * sqrt( g_gravity.value / ( 3 * length ) );
  1450.  
  1451.     ent->s.pos.trDuration = ( 1000 / freq );
  1452.  
  1453.     InitMover( ent );
  1454.  
  1455.     VectorCopy( ent->s.origin, ent->s.pos.trBase );
  1456.     VectorCopy( ent->s.origin, ent->r.currentOrigin );
  1457.  
  1458.     VectorCopy( ent->s.angles, ent->s.apos.trBase );
  1459.  
  1460.     ent->s.apos.trDuration = 1000 / freq;
  1461.     ent->s.apos.trTime = ent->s.apos.trDuration * phase;
  1462.     ent->s.apos.trType = TR_SINE;
  1463.     ent->s.apos.trDelta[2] = speed;
  1464. }
  1465.